#!/bin/sh

HERE="$(dirname "$(readlink -f "${0}")")"

MAIN=$(grep -r "^Exec=.*" "$HERE"/*.desktop | head -n 1 | cut -d "=" -f 2 | cut -d " " -f 1)

############################################################################################
# Use bundled paths
############################################################################################

export PATH="${HERE}"/usr/bin/:"${HERE}"/usr/sbin/:"${HERE}"/usr/games/:"${HERE}"/bin/:"${HERE}"/sbin/:"${PATH}"
export XDG_DATA_DIRS="${HERE}"/usr/share/:"${XDG_DATA_DIRS}"

############################################################################################
# Use bundled Python
############################################################################################

export PYTHONHOME="${HERE}"/usr/

############################################################################################
# Use bundled Tcl/Tk
############################################################################################

if [ -e "${HERE}"/usr/share/tcltk/tcl8.6 ] ; then
  export TCL_LIBRARY="${HERE}"/usr/share/tcltk/tcl8.6:$TCL_LIBRARY:$TK_LIBRARY
  export TK_LIBRARY="${HERE}"/usr/share/tcltk/tk8.6:$TK_LIBRARY:$TCL_LIBRARY
fi

############################################################################################
# Make it look more native on Gtk+ based systems
############################################################################################

case "${XDG_CURRENT_DESKTOP}" in
    *GNOME*|*gnome*)
        export QT_QPA_PLATFORMTHEME=gtk2
esac

############################################################################################
# If .ui files are in the AppDir, then chances are that we need to cd into usr/
# because we may have had to patch the absolute paths away in the binary
############################################################################################

UIFILES=$(find "$HERE" -name "*.ui")
if [ ! -z "$UIFILES" ] ; then
  cd "$HERE/usr"
fi

############################################################################################
# Use bundled GStreamer
# NOTE: May need to remove libgstvaapi.so
############################################################################################

if [ ! -z $(find "${HERE}" -name "libgstcoreelements.so" -type f) ] ; then
  export GST_PLUGIN_PATH=$(dirname $(readlink -f $(find "${HERE}" -name "libgstcoreelements.so" -type f | head -n 1)))
  export GST_PLUGIN_SCANNER=$(find "${HERE}" -name "gst-plugin-scanner" -type f | head -n 1)
  export GST_PLUGIN_SYSTEM_PATH=$GST_PLUGIN_PATH
  env | grep GST
fi

############################################################################################
# Run experimental bundle that bundles everything if a private ld-linux-x86-64.so.2 is there
# This allows the bundle to run even on older systems than the one it was built on
############################################################################################

cd "$HERE/usr" # Not all applications will need this; TODO: Make this opt-in

# Try to find a binary with the same name as the AppImage or the symlink through which
# it was invoked, without any suffix
if [ -z "$ARGV0" ] ; then
  # AppRun is being executed outside of an AppImage
  ARGV0="$0"
fi
BINARY_NAME=$(basename "$ARGV0")
if [ "$BINARY_NAME" = "AppRun" ] ; then
  unset BINARY_NAME
fi

BINARY_NAME="${BINARY_NAME%.*}" # remove everything after the last "."
MAIN_BIN=$(find "$HERE/usr/bin" -name "$BINARY_NAME" | head -n 1)

# Fall back to finding the main binary based on the Exec= line in the desktop file
if [ -z "$MAIN_BIN" ] ; then
  MAIN_BIN=$(find "$HERE/usr/bin" -name "$MAIN" | head -n 1)
fi

LD_LINUX=$(find "$HERE" -name 'ld-*.so.*' | head -n 1)

if [ -e "$LD_LINUX" ] ; then
  export GCONV_PATH="$HERE/usr/lib/gconv"
  export FONTCONFIG_FILE="$HERE/etc/fonts/fonts.conf"
  export GTK_PATH=$(find "$HERE/lib" -name gtk-* -type d)
  export GTK_THEME=Default # This one should be bundled so that it can work on systems without Gtk
  export GDK_PIXBUF_MODULEDIR=$(find "$HERE" -name loaders -type d -path '*gdk-pixbuf*')
  export GDK_PIXBUF_MODULE_FILE=$(find "$HERE" -name loaders.cache -type f -path '*gdk-pixbuf*') # Patched to contain no paths
  export XDG_DATA_DIRS="${HERE}"/usr/share/:"${XDG_DATA_DIRS}"
  export PERLLIB="${HERE}"/usr/share/perl5/:"${HERE}"/usr/lib/perl5/:"${PERLLIB}"
  export GSETTINGS_SCHEMA_DIR="${HERE}"/usr/share/glib-2.0/runtime-schemas/:"${HERE}"/usr/share/glib-2.0/schemas/:"${GSETTINGS_SCHEMA_DIR}"
  export QT_PLUGIN_PATH="$(readlink -f "$(dirname "$(find "${HERE}" -type d -path '*/plugins/platforms' 2>/dev/null)" 2>/dev/null)" 2>/dev/null)"
  case $line in
    "ld-linux"*) exec "${LD_LINUX}" --inhibit-cache "${MAIN_BIN}" "$@" ;;
    *) exec "${LD_LINUX}" "${MAIN_BIN}" "$@" ;;
  esac
else
  exec "${MAIN_BIN}" "$@"
fi
